package com.sloth.service.impl;

import com.sloth.mapper.AddressMapper;
import com.sloth.mapper.OrderDetailsMapper;
import com.sloth.mapper.ProductMapper;
import com.sloth.pojo.Address;
import com.sloth.pojo.OrderDetails;
import com.sloth.pojo.OrderDetailsExample;
import com.sloth.pojo.Product;
import com.sloth.service.ReadyPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lai
 * @time 2021/11/6 22:58
 */
@Service
public class ReadyPayServiceImpl implements ReadyPayService {

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private OrderDetailsMapper orderDetailsMapper;

    @Autowired
    private ProductMapper productMapper;

    // 通过收货地址id查询收货信息
    @Override
    public Address getAddressByAddressId(Integer addressId) {
        Address address = addressMapper.selectByPrimaryKey(addressId);
        return address;
    }

    // 通过订单id查询订单详情，再通过订单详情中的商品id查询商品信息，返回商品信息集合
    @Override
    public List<Product> getProductListByOrderId(Integer orderId) {

        List<Product> productList = new ArrayList<>();

        OrderDetailsExample orderDetailsExample = new OrderDetailsExample();
        orderDetailsExample.createCriteria().andOrderIdEqualTo(orderId);
        List<OrderDetails> orderDetailsList = orderDetailsMapper.selectByExample(orderDetailsExample);
        for (OrderDetails orderDetails : orderDetailsList){
            // 通过订单详情中的商品id查询商品
            Product product = productMapper.selectByPrimaryKey(orderDetails.getProId());
            productList.add(product);
        }
        return productList;
    }

}